package com.salim.leetcode.$3;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-7-20
 * Time: 下午11:02
 * To change this template use File | Settings | File Templates.
 */
public class LongestSubString {
    public int lengthOfLongestSubstring(String s) {
        int start = 0;
        int end = 0;
        int maxLength = 0;
        Boolean[] exists = new Boolean[256];

        //生成exists数组
        for(int ii=0;ii<exists.length;ii++){
            exists[ii] = false;
        }

        while(end < s.length() ){
            if(!exists[s.charAt(end)] ){
                exists[s.charAt(end)] = true;
                end++;
            }else{
                //碰到重复的 记录一下之前一个字符串的最大值
                maxLength = maxLength > end - start ? maxLength : end - start;
                while(start< end && s.charAt(start) !=s.charAt(end)){
                    exists[s.charAt(start)] = false;
                    start++;
                }
                start++;
                end++;
            }

        }
        maxLength = maxLength > end - start ? maxLength : end - start ;
        return maxLength;
    }

    public static void main(String[] args){
        String str ="dvabv";
        LongestSubString temp = new LongestSubString();
        System.out.println(temp.lengthOfLongestSubstring(str));
    }
}
